﻿using System;
using System.Collections.Generic;
using System.IO;
using ProjScan.Config;
using ProjScan.Config.GUIConfig;
using ProjScan.Tools;
using UnityEngine;

namespace ProjScan.ScanGUI
{
	// Token: 0x02000253 RID: 595
	public class ScanOverviewManager : Singleton<ScanOverviewManager>
	{
		// Token: 0x1700036F RID: 879
		// (get) Token: 0x06001014 RID: 4116 RVA: 0x000483E8 File Offset: 0x000465E8
		public string ScanResultsOverviewPath
		{
			get
			{
				return Path.Combine(ToolsHelper.GetScanOutPut(), "ResultOverview.csv");
			}
		}

		// Token: 0x17000370 RID: 880
		// (get) Token: 0x06001015 RID: 4117 RVA: 0x00007B80 File Offset: 0x00005D80
		public bool ExistScanResultsOverview
		{
			get
			{
				return File.Exists(this.ScanResultsOverviewPath);
			}
		}

		// Token: 0x06001016 RID: 4118 RVA: 0x00007B8D File Offset: 0x00005D8D
		public void Init()
		{
			this.modulesRunError.Clear();
		}

		// Token: 0x06001017 RID: 4119 RVA: 0x0004840C File Offset: 0x0004660C
		public bool CheckModulesRunError(string moduleName)
		{
			bool flag2;
			bool flag = this.modulesRunError.TryGetValue(moduleName, out flag2);
			return flag && flag2;
		}

		// Token: 0x06001018 RID: 4120 RVA: 0x00007B9C File Offset: 0x00005D9C
		public void SetModulesRunError(string moduleName)
		{
			this.modulesRunError[moduleName] = true;
		}

		// Token: 0x06001019 RID: 4121 RVA: 0x00048438 File Offset: 0x00046638
		private bool CheckRuleSuccess(string ruleId)
		{
			Dictionary<string, FailedAssetInfo> dictionary;
			bool flag = FailedResultManager.Instance.dicFails.TryGetValue(ruleId, out dictionary);
			return !flag || dictionary.Count == 0;
		}

		// Token: 0x0600101A RID: 4122 RVA: 0x00048470 File Offset: 0x00046670
		private float GetScoreByRulePriority(RulePriority rulePriority)
		{
			float result;
			switch (rulePriority)
			{
			case RulePriority.None:
				result = 1.5f;
				break;
			case RulePriority.Custom:
				result = 1f;
				break;
			case RulePriority.Middle:
				result = 2f;
				break;
			case RulePriority.High:
				result = 3f;
				break;
			default:
				result = 0f;
				break;
			}
			return result;
		}

		// Token: 0x0600101B RID: 4123 RVA: 0x000484C4 File Offset: 0x000466C4
		public void OutputScanResultOverviewCSV(Dictionary<string, bool> modulesToRun)
		{
			float num = 0f;
			float num2 = 0f;
			Type[] array = new Type[]
			{
				typeof(BasicAssetsCheckConfig),
				typeof(ScenesCheckConfig),
				typeof(GlobalSettingsConfig),
				typeof(CSCheckConfig),
				typeof(EffectsPlayCheckConfig),
				typeof(ShaderAnalysisConfig),
				typeof(ArtAssetsCheckConfig)
			};
			string[] array2 = new string[]
			{
				"Basic_Assets_Check",
				"Scenes_Check",
				"Global_Settings_Check",
				"CS_Check",
				"Effects_Play_Check",
				"Shader_Analysis",
				"Art_Assets_Check"
			};
			int[] array3 = new int[7];
			int[] array4 = new int[7];
			for (int i = 0; i < array.Length; i++)
			{
				string text = array2[i];
				bool flag = !modulesToRun[text];
				if (flag)
				{
					ScanLog.UnityLog(text + " 被关闭!!! 跳过该模块的数据统计!");
				}
				Type baseConfigType = array[i];
				BaseConfig config = Singleton<ConfigMgr>.Instance.GetConfig(baseConfigType);
				foreach (KeyValuePair<string, RuleConfig> keyValuePair in config.dicRuleConfig)
				{
					string key = keyValuePair.Key;
					RuleConfig value = keyValuePair.Value;
					RuleInfo ruleInfo = Singleton<RuleInfoManager>.Instance.dicRuleInfoByID[key];
					bool flag2 = !modulesToRun[text];
					if (!flag2)
					{
						bool checkEnabled = value.checkEnabled;
						if (checkEnabled)
						{
							num2 += this.GetScoreByRulePriority(ruleInfo.RulePriority);
							array3[i]++;
						}
						bool flag3 = this.CheckModulesRunError(text);
						if (!flag3)
						{
							bool flag4 = value.checkEnabled && this.CheckRuleSuccess(key);
							if (flag4)
							{
								array4[i]++;
								num += this.GetScoreByRulePriority(ruleInfo.RulePriority);
							}
						}
					}
				}
				bool flag5 = this.CheckModulesRunError(text);
				if (flag5)
				{
					ScanLog.UnityLog(text + " 扫描过程中出错，将其置为 -1/开启规则数");
					array4[i] = -1;
				}
			}
			string basicAssetsCheck = string.Format("{0}/{1}", array4[0], array3[0]);
			string scenesCheck = string.Format("{0}/{1}", array4[1], array3[1]);
			string globalSettingsCheck = string.Format("{0}/{1}", array4[2], array3[2]);
			string csCheck = string.Format("{0}/{1}", array4[3], array3[3]);
			string effectsPlayCheck = string.Format("{0}/{1}", array4[4], array3[4]);
			string shaderAnalysis = string.Format("{0}/{1}", array4[5], array3[5]);
			string artAssetsCheck = string.Format("{0}/{1}", array4[6], array3[6]);
			string scoreByRulePriority = string.Format("{0}/{1}", num, num2);
			this.WriteScanResults(scoreByRulePriority, basicAssetsCheck, scenesCheck, globalSettingsCheck, csCheck, effectsPlayCheck, shaderAnalysis, artAssetsCheck);
		}

		// Token: 0x0600101C RID: 4124 RVA: 0x00048814 File Offset: 0x00046A14
		public ScanOverviewData[] ReadScanResults()
		{
			string scanResultsOverviewPath = this.ScanResultsOverviewPath;
			bool flag = !this.ExistScanResultsOverview;
			ScanOverviewData[] result;
			if (flag)
			{
				Debug.Log("Overview file " + scanResultsOverviewPath + " does not exist.");
				result = Array.Empty<ScanOverviewData>();
			}
			else
			{
				List<ScanOverviewData> list = new List<ScanOverviewData>();
				using (FileStream fileStream = File.OpenRead(scanResultsOverviewPath))
				{
					using (StreamReader streamReader = new StreamReader(fileStream))
					{
						string a = streamReader.ReadLine();
						bool flag2 = a != "Time,Version,ScoreByRulePriority,Overall,BasicAssetsCheck,ScenesCheck,GlobalSettingsCheck,CSCheck,EffectsPlayCheck,ShaderAnalysis,ArtAssetsCheck";
						if (flag2)
						{
							throw new Exception("Overview file " + scanResultsOverviewPath + " is not in the correct format.");
						}
						while (!streamReader.EndOfStream)
						{
							string text = streamReader.ReadLine();
							string[] array = text.Split(new char[]
							{
								','
							});
							ScanOverviewData item = new ScanOverviewData
							{
								Time = array[0],
								Version = array[1],
								ScoreByRulePriority = array[2],
								Overall = array[3],
								BasicAssetsCheck = array[4],
								ScenesCheck = array[5],
								GlobalSettingsCheck = array[6],
								CSCheck = array[7],
								EffectsPlayCheck = array[8],
								ShaderAnalysis = array[9],
								ArtAssetsCheck = array[10]
							};
							list.Add(item);
						}
					}
				}
				result = list.ToArray();
			}
			return result;
		}

		// Token: 0x0600101D RID: 4125 RVA: 0x000489C4 File Offset: 0x00046BC4
		private void WriteScanResults(string scoreByRulePriority, string basicAssetsCheck, string scenesCheck, string globalSettingsCheck, string csCheck, string effectsPlayCheck, string shaderAnalysis, string artAssetsCheck)
		{
			bool flag = File.Exists(this.ScanResultsOverviewPath);
			FileMode mode = flag ? FileMode.Append : FileMode.Create;
			using (FileStream fileStream = File.Open(this.ScanResultsOverviewPath, mode))
			{
				using (StreamWriter streamWriter = new StreamWriter(fileStream))
				{
					bool flag2 = !flag;
					if (flag2)
					{
						streamWriter.WriteLine("Time,Version,ScoreByRulePriority,Overall,BasicAssetsCheck,ScenesCheck,GlobalSettingsCheck,CSCheck,EffectsPlayCheck,ShaderAnalysis,ArtAssetsCheck");
					}
					string text = DateTime.Now.ToString();
					string version = Singleton<ScanInfo>.Instance.version;
					string text2 = this.CalculateOverall(new string[]
					{
						basicAssetsCheck,
						scenesCheck,
						globalSettingsCheck,
						csCheck,
						effectsPlayCheck,
						shaderAnalysis,
						artAssetsCheck
					});
					string[] value = new string[]
					{
						text,
						version,
						scoreByRulePriority,
						text2,
						basicAssetsCheck,
						scenesCheck,
						globalSettingsCheck,
						csCheck,
						effectsPlayCheck,
						shaderAnalysis,
						artAssetsCheck
					};
					streamWriter.WriteLine(string.Join(",", value));
				}
			}
		}

		// Token: 0x0600101E RID: 4126 RVA: 0x00048AEC File Offset: 0x00046CEC
		public string CalculateOverall(string[] checks)
		{
			int num = 0;
			int num2 = 0;
			foreach (string text in checks)
			{
				string[] array = text.Split(new char[]
				{
					'/'
				});
				bool flag = array.Length == 2;
				if (flag)
				{
					num += int.Parse(array[0]);
					num2 += int.Parse(array[1]);
				}
			}
			return string.Format("{0}/{1}", num, num2);
		}

		// Token: 0x04000978 RID: 2424
		private const string csvHeader = "Time,Version,ScoreByRulePriority,Overall,BasicAssetsCheck,ScenesCheck,GlobalSettingsCheck,CSCheck,EffectsPlayCheck,ShaderAnalysis,ArtAssetsCheck";

		// Token: 0x04000979 RID: 2425
		private Dictionary<string, bool> modulesRunError = new Dictionary<string, bool>();
	}
}
